/**
 * 身份证信息上传
 */
(function(){
    var _isAjaxIdUpload = false;
    var _uidData = {
        stateId: '',
        idNum: '',
        idName: '',
        frontpic: '',
        backpic:''
    }
    var isPopFirst = true;

    function getValidateMsg(){
        if(_uidData.idName == '') {
            return '请输入真实姓名！';
        } else{
            tips = mbValidator.isRealname(_uidData.idName);
            if(tips != '') {
                return '真实姓名'+tips;
            }
        }
        if(_uidData.idNum == '') {
            return '请输入身份证号！';
        } else {
            tips = mbValidator.isValdId(_uidData.idNum);
            if(tips != '') {
                return tips;
            }
        }
        if(_uidData.stateId == 2){
            if(_uidData.frontpic == '') {
                return '请上传身份证正面！';
            }
            if(_uidData.backpic == '') {
                return '请上传身份证反面！';
            }
        }
        return "";
    }

    function _IDAjaxUpload(){
        _uidData.idName = $('#yc_sf_name').val().trim();
        _uidData.idNum = $('#yc_sf_id').val().trim();
        _uidData.frontpic = $('#ycSfFrontPic').val().trim();
        _uidData.backpic = $('#ycSfBackPic').val().trim();
        var tip = getValidateMsg();
        if(tip != ""){
            _global.promote.alertPopup(tip, true);
            return;
        }
        var _data;
        if(_uidData.stateId == 2){
            _data = {"identity_name" : _uidData.idName, "identity_card_num" : _uidData.idNum, "identity_photo" : _uidData.frontpic, "identity_photo_1" : _uidData.backpic};
        }else if(_uidData.stateId == 0){
            _data = {"identity_name" : _uidData.idName, "identity_card_num" : _uidData.idNum};
        }
        if(_isAjaxIdUpload) {
            return;
        }
        _isAjaxIdUpload = true;
        $.ajax({
            type:'post',
            url:'/Home/Carts/uploadIdentity'+getFixedExtraUrl(),
            dataType: 'json',
            data:_data ,
            success:function(rs){
                _global.promote.alertPopup(rs.msg);
                if(rs.status == '1'){
                    window.location.reload();
                }
                _isAjaxIdUpload = false;
            },
            error:function(){
                _isAjaxIdUpload = false;
                _global.promote.alertPopup('服务器异常请稍后重试！');
            }
        })
    }

/**
  * 选择图片
  */
 
    $("#ycSfFrontBtn").on("click",function(e){
        e.preventDefault();
        var _self = $(this);
         _global.uploadId.selectImage(_self);
    })
    $("#ycSfBackBtn").on("click",function(e){
        e.preventDefault();
         var _self = $(this);
         _global.uploadId.selectImage(_self);
    })

    function uploadClickEvent(){
        if(_uidData.stateId == 2){
            $('.identitly-state').show();
        }else{
            $('.identitly-state').hide();
        }
        _global.promote.popup('.upload-identity-frame', !isPopFirst);
        if(isPopFirst){
            isPopFirst = false;
        }
    }

    var isFromInner = typeof _ycIdData === "undefined" ? false : true;
    if(isFromInner){
        $("#yc_sf_name").val(_ycIdData.idName);
        $("#yc_sf_id").val(_ycIdData.idNum);
        _uidData.stateId = _ycIdData.yzType == 1 ? 0 : 2;
        if(_ycIdData.from == "order"){
            $("#ycOrderIdTip").css("display", "block");
        }else{
            $("#ycInfoIdNum").html(_ycIdData.idNum);
        }
        $('#ycIdUploadBtn').on('click', function(e) {
            e.preventDefault();
            uploadClickEvent();
        });
    }else{
        $('.upload-identity').on('click', function(e) {
            e.preventDefault();
            _uidData.stateId = parseInt($(this).data('state-id'));
            uploadClickEvent();
        });
    }
    $('#ycSfUpload').on('click', function(e){
        e.preventDefault();
        _IDAjaxUpload();
    });
})();